﻿Imports TodoMicroSolutionPackV1.dal.dto
Imports TodoMicroSolutionPackV1.entidades.dal.dto
Imports TodoMicroSolutionPackV1.dal.dao
Imports TodoMicroSolutionPackV1
Imports NHibernate

Public Class frmEntidadSelect
    Private cliente As entidad
    Private clientList As IList(Of entidad)
    Private _session As ISession
    Private _DAOFactory As NhibernateDAOFactory

    Protected ReadOnly Property Session As ISession
        Get
            If _session Is Nothing Then
                _session = NhibernateSessionFactory.getSession
            End If
            Return _session
        End Get
    End Property

    Protected ReadOnly Property DAOFactory As NhibernateDAOFactory
        Get
            If _DAOFactory Is Nothing Then
                _DAOFactory = New NhibernateDAOFactory(Session)
            End If
            Return _DAOFactory
        End Get
    End Property
    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click
        Dim numero_documento As Int64 = 0
        Dim id As Integer = 0
        If IsNumeric(txtCUIT.Text) Then numero_documento = Convert.ToInt64(txtCUIT.Text)
        If IsNumeric(txtId.Text) Then id = CInt(txtId.Text)

        clientList = DAOFactory.getEntidadDAO.buscarCliente(txtRazonSocial.Text, numero_documento, txtNombre.Text, txtApellido.Text, txtEmail.Text, id)
        dataGrid.DataSource = clientList
    End Sub

    Public Function getCliente(ByRef session As ISession) As entidad
        _session = session
        clientList = DAOFactory.getEntidadDAO.buscarCliente("consumidor final", 0, "", "", "", 0)
        dataGrid.DataSource = clientList
        Me.ShowDialog()
        Return Me.cliente
    End Function

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        cliente = clientList.Item(dataGrid.CurrentRow.Index)
        Me.Dispose()
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        cliente = Nothing
        Me.Dispose()
    End Sub

    Private Sub frmEntidadSelect_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
    End Sub

    Private Sub frmEntidadSelect_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Return Then
            btnBuscar.PerformClick()
        End If
    End Sub

    Private Sub frmEntidadSelect_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
    End Sub
End Class